package com.hxh.wechat.util

import com.hxh.wechat.constant.GlobalConstant
import okhttp3.*
import okhttp3.logging.HttpLoggingInterceptor
import java.util.concurrent.TimeUnit

class HttpUtil {
    companion object {
        val BASE_URL = "http://${GlobalConstant.URL}:8888/wh_sys";

        //        private const val BASE_URL = "http://192.168.43.35:8888/wh_sys"
        private val httpLoggingInterceptor =
            HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC)
        private val client = OkHttpClient.Builder()
            .addInterceptor(httpLoggingInterceptor)
            .readTimeout(5, TimeUnit.SECONDS).build()

        // 发送GET请求
        fun get(path: String): Call? {
            val url = "$BASE_URL/$path"
            val request = Request.Builder()
                .url(url)
                .build()
            return client.newCall(request)
        }

        fun getFullPath(path: String): Call? {
            val request = Request.Builder()
                .url(path)
                .build()
            return client.newCall(request)
        }

        // 发送POST请求
        fun post(path: String, formBody: RequestBody): Call? {
            val url = "$BASE_URL/$path"
            val request = Request.Builder()
                .url(url)
                .header("Content-Type", "application/json")
                .post(formBody)
                .build()
            return client.newCall(request)
        }
    }
}